package com.zrrd.jt_admin.shop.sku.controller;


import com.zrrd.jt_admin.common.entity.Skus;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.SkusPageVo;
import com.zrrd.jt_admin.shop.sku.service.SkusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-07-02
 */
@Api(tags = "商品规格管理-接口文档")
@RestController
@RequestMapping("admin/skus")
@ResponseBody
public class SkusController {

    @Resource
    private SkusService skusService;

    @ApiOperation(value = "商品规格列表")
    @GetMapping("{page}")
    public ResponseResult findByPage(@PathVariable Integer page,
                                     @RequestParam(required = false) Integer limit) {
        if (limit == null){
            limit = 5;
        }
        SkusPageVo skusPageVo = skusService.findByPage(page, limit);
        return ResponseResult.queryResponse(skusPageVo);
    }

    @ApiOperation(value = "增加商品规格")
    @PostMapping
    public ResponseResult addSkus(@RequestBody Skus skus) {
        return ResponseResult.addResponse(skusService.save(skus));
    }

    @ApiOperation(value = "修改商品规格")
    @PutMapping
    public ResponseResult updateSkus(@RequestBody Skus skus) {
        return ResponseResult.updateResponse(skusService.updateById(skus));
    }

    @ApiOperation(value = "修改商品规格状态")
    @PutMapping("{id}/updateStatus")
    public ResponseResult updateStatusSkus(@PathVariable Integer id,Integer status){
        boolean bstatus=status==1?true:false;
        boolean flag = skusService.lambdaUpdate()
                .eq(Skus::getId, id)
                .set(Skus::getStatus, status)
                .update();
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除商品规格")
    @DeleteMapping("{id}/delete")
    public ResponseResult deleteSkus(@PathVariable Integer id) {
        return ResponseResult.removeResponse(skusService.removeById(id));
    }

    @ApiOperation(value = "批量删除商品规格")
    @DeleteMapping("deleteAll")
    public ResponseResult deleteAllSkus(@RequestBody List<Integer> ids) {
        return ResponseResult.removeResponse(skusService.removeByIds(ids));
    }

}

